<?php

require ("sharedResources.php");

function printInfoAndDownload($sid, $document) {
      
      global $documentClient;

			$docId = $document->id;
			echo 'DocId: ' . $document->id . PHP_EOL;
			echo 'Title: ' . $document->title . PHP_EOL;
			echo 'size: ' . $document->fileSize . PHP_EOL;
			
			// 2) Launch a query to get the document info (fileName)
			$ddi = array ('sid' => $sid, 'docId' => $docId );
			//$docresult = $documentClient->getDocument ( $ddi ); (Since 6.5 non need to perform another query)
			//$myFileName = $docresult->document->fileName;
			$myFileName = $document->fileName;
			
			// 3) Download the document to the tmp folder
			$contentresult = $documentClient->getContent ( $ddi );
			
			// get the binary content from the result response
			$content = $contentresult->return;
			
			// Finally write the document on disk
			$myFile = "C:\\tmp\\" . $myFileName;
			echo 'Writing content to: ' . $myFile . PHP_EOL;
			
			$fh = fopen ( $myFile, 'w' ) or die ( "can't open file" );
			fwrite ( $fh, $content );
			fclose ( $fh );
			
			echo '----------------' . PHP_EOL;
}

$sid = '';
try {
	// We need a SID (session ID) in order to operate
	$sid = login ( 'admin', 'admin' );
	echo 'sid: ' . $sid . PHP_EOL;
	
	$options ['expression'] = 'national government'; // this is the full-text query
	$options ['expressionLanguage'] = 'en'; // this is the language of the query (en == english)
	$options ['language'] = 'en'; // this is the language of the documents we are looking for
	$options ['maxHits'] = 4; // Max number of results we want to retrieve

	// Required but not checked
	$options ['type'] = 0; // 0 is full-text query
	$options ['userId'] = 0; // not used
	$options ['searchInSubPath'] = false;
	
	// New from 6.4
	$options ['depth'] = 0; // Folder depth
  
  // New from 6.5.2
  $options ['retrieveAliases'] = 0; // requested but not used
  $options ['caseSensitive'] = 0; // requested but not used
	
	$searchParams = array ('sid' => $sid, 'options' => $options );
	
	// 1) Perform a full-text query
	$result = $searchClient->find ( $searchParams );
	print_r ( $result );
	
	echo PHP_EOL . 'estimated Hits Number: ' . $result->searchResult->estimatedHitsNumber . PHP_EOL . PHP_EOL;
	
	// Cycle over the results and download all the documents to the tmp folder
	if (! empty ( $result->searchResult->hits )) {
	
		echo 'Search returned something'. PHP_EOL;
		$documents = $result->searchResult->hits;
    if (sizeof ( $documents ) == 1) {
      echo 'Search returned just 1 document'. PHP_EOL;      
      printInfoAndDownload($sid, $documents);
      return;
    }        		
		
		foreach ( $result->searchResult->hits as $hit ) {	
		  printInfoAndDownload($sid, $hit);
		}
	} else {
		echo 'Your search did not match any documents';
	}
	
//unset ( $folderClient );
} catch ( SoapFault $e ) {
	print_r ( $e );
}

// Always we need to logout in order to release connection slot
logout ( $sid );
?> 
